package com.blb.community.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.community.Annotation.Log;
import com.blb.community.entity.ZyCommunity;
import com.blb.community.entity.vo.CommunityAreaVO;
import com.blb.community.entity.vo.CommunityVO;
import com.blb.community.service.IZyCommunityService;
import com.blb.community.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 小区  前端控制器
 * </p>
 *
 * @author 帅帅
 * @since 2022-07-02
 */
@Api(tags = "小区管理模块")
@RestController
@RequestMapping("/zy-community")
public class ZyCommunityController {

    @Autowired
    private IZyCommunityService communityService;

//    @ApiOperation("小区分页查询")
//    @PostMapping("/communitys/page")
//    public ResponseResult<IPage<CommunityVO>> getCommunity(CommunityVO communityVO) {
//        IPage<CommunityVO> community = communityService.getCommunity(communityVO, new Page<>(communityVO.getCurrent(), 5L));
//        return ResponseResult.ok(community);
//    }

    @ApiOperation("小区分页查询t")
    @PostMapping("/communitys/page")
    public ResponseResult<IPage<CommunityAreaVO>> getCommunityPage(CommunityAreaVO communityAreaVO) {
        IPage<CommunityAreaVO> communityArea = communityService.getCommunityArea(communityAreaVO, new Page<>(communityAreaVO.getCurrent(), 5L));
        return ResponseResult.ok(communityArea);
    }
    @Log(module = "小区添加", operationType = 1, requestMethod = "POST")
    @ApiOperation("小区添加")
    @PostMapping("/community")
    public ResponseResult<String> addCommunity(@RequestBody CommunityAreaVO communityAreaVO){
        communityAreaVO.setCommunityCode("COMMUNITY_16079400742945");
        communityService.addCommunity(communityAreaVO);
        return ResponseResult.ok("添加成功");
    }


    @Log(module = "小区更新", operationType = 2, requestMethod = "PUT")
    @ApiOperation("小区更新")
    @PutMapping("/community")
    public ResponseResult<String> updateCommunity(@RequestBody CommunityAreaVO communityAreaVO){
        communityAreaVO.setCommunityCode("COMMUNITY_16079400742945");
        communityService.updateCommunity(communityAreaVO);
        return ResponseResult.ok("更新成功");
    }

    @Log(module = "小区单删", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("小区单删")
    @DeleteMapping("/community/{id}")
    public ResponseResult<String> delCommunity(@PathVariable Long id){
        communityService.removeById(id);
        return ResponseResult.ok("删除成功");
    }
    @Log(module = "小区单删", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("小区单删")
    @DeleteMapping("/communitys/{ids}")
    public ResponseResult<String> delAllCommunity(@PathVariable ArrayList<Long> ids){
        communityService.removeByIds(ids);
        return ResponseResult.ok("删除成功");
    }
    @GetMapping("communitys")
    public ResponseResult<List<ZyCommunity>> getList(){
        return ResponseResult.ok(communityService.list());
    }

    @Log(module = "删除小区", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("删除小区")
    @PutMapping("/dept")
    public ResponseResult<String> updateDeptId(CommunityVO community){
        communityService.updateDeptId(community.getCommunityId(),community.getDeptId());
        return ResponseResult.ok("更新成功");
    }

}
